package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.union.huaben.UnionHuaBenPassWeekData;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenRankMgr;
import com.yanqu.road.server.manager.union.huaben.rank.CrossGroupWeekUnionRankListModel;
import com.yanqu.road.server.manger.UnionHuaBenServerGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

@Cmd(code = Protocol.C_CROSS_UNION_HUA_BEN_RANK, desc = "话本排行")
public class CrossUnionHuaBenUnionWeekRankListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionHuaBenProto.UnionHuaBenUnionWeekRankListReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenUnionWeekRankListReqMsg.parseFrom(packet.getBytes());
        int type = reqMsg.getType();
        int degree = reqMsg.getDegree();

        UnionHuaBenProto.UnionHuaBenUnionWeekRankListRespMsg.Builder respMsg = UnionHuaBenProto.UnionHuaBenUnionWeekRankListRespMsg.newBuilder();
        respMsg.setDegree(degree);
        respMsg.setType(type);

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();
        if(context.getRet() != 0){
            respMsg.setRet(context.getRet());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_RANK, respMsg));
            return;
        }

        int groupId;
        int weekId;
        if(type == 1){
            int thisWeekId = CrossUnionHuaBenConfigMgr.getThisWeekId();
            if(CrossUnionHuaBenRankMgr.isSunDayLast4Hour()){
                groupId = CrossUnionHuaBenRankMgr.getWeekServerGroup(thisWeekId, serverId);
            }else {
                groupId = UnionHuaBenServerGroupMgr.getGroupIdByServerId(serverId);
            }
            weekId = thisWeekId;
        }else {
            int preWeekId = CrossUnionHuaBenConfigMgr.getPreWeekId();
            groupId = CrossUnionHuaBenRankMgr.getWeekServerGroup(preWeekId, serverId);
            weekId = preWeekId;
        }

        respMsg.setWeekId(weekId);
        respMsg.setRet(0);
        respMsg.setMyRank(-1);
        respMsg.setValue("-1");

        CrossGroupWeekUnionRankListModel model = CrossUnionHuaBenRankMgr.getCrossGroupWeekUnionRankListModel(weekId, groupId, degree);
        if(model != null){
            respMsg.setMyRank(model.getMyRank(context.getUnionInfo().getUnionUid()));
            respMsg.addAllRankList(model.getRankListMsg());
        }

        UnionHuaBenPassWeekData weekData = CrossUnionHuaBenMgr.getUnionHuaBenPassWeekData(weekId, context.getUnionInfo().getServerId(), context.getUnionInfo().getUnionUid());
        if(weekData != null && weekData.getDegree() == degree){
            int passNum = CrossUnionHuaBenMgr.getPassNum(weekData.getPassList());
            respMsg.setValue(""+ passNum);
            int passTime = CrossUnionHuaBenMgr.calHuaBenPassTime(DateHelper.getCurrentSecond(), weekData);
            if(passNum > 0) {
                respMsg.setParam("" + passTime);
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_RANK, respMsg));
    }

    @Override
    public void sendData() {

    }
}
